"use strict";

(function() {
	alx.svg.Canvas = function(document, args) {
		var me       = this, 
				disposed = false, 
				canvas   = null, 
				children = [];

		var init = function() {
			canvas = document.createElementNS(alx.svg.ns, "svg");

			canvas.setAttribute("height", getDocumentHeight());
			canvas.setAttribute("style", "position:absolute;top:0;left:0;z-index:10000");
		};


		me.attach = function () {
			document.body.appendChild(canvas);
		};
		
		me.detach = function () {
			canvas && canvas.parentNode && canvas.parentNode.removeChild(canvas);
			//me.attach();
		};
		
		me.disposed = false;
		me.dispose = function() {
			if (!me.disposed) {
				canvas && canvas.parentNode && canvas.parentNode.removeChild(canvas);
				canvas = null;
				children = null;
				
				me.disposed = true;
			}
		};

		me.append = function(svgElem) {
			var domElem = null;

			if ((svgElem instanceof alx.svg.Polyline) || (svgElem instanceof alx.svg.Rect)) {
				domElem = svgElem.getElement();
			}

			if (domElem && canvas) {
				children.push(svgElem);
				canvas.appendChild(domElem);
			}
		};

		me.remove = function(svgElem) {
			
		};

		me.getElement = function() {
			return canvas;
		};

		me.clear = function() {
			/*foreachChildren(function(index) {
				var child = this;

				child.parentNode && child.parentNode.removeChild(child);
				delete children[index];
			});*/
		};

		me.isAttached = function () {
			if (canvas.parentNode) return true;
			return false;
		};
		
		// Private stuff

		var getDocumentHeight = function() {
			var D = document;

			return Math.max(
							 Math.max(D.body.scrollHeight, D.documentElement.scrollHeight), 
						 	 Math.max(D.body.offsetHeight, D.documentElement.offsetHeight), 
						 	 Math.max(D.body.clientHeight, D.documentElement.clientHeight)
						 );
		};

		// ------------------------------------------------------------------ //
		init();
	};
})();